<template>
	<view class="diamond-page">
		<!-- #ifdef APP -->
		<u-navbar title="钻石" :autoBack="true" :placeholder="true">></u-navbar>
		<!-- #endif -->
		<view class="header">
			<view class="title-wrapper">
				<view class="diamond-title">
					<image class="diamond-icon" src="../../static/mine/icon_zs.png" />
					<text class="title">钻石</text>
					<text class="count">(个)</text>
				</view>
				<view class="detail-btn" @click="$Tips('/pages/mine/diamond-details')">
					<text class="detail-text">查看明细</text>
				</view>
			</view>
			<view class="diamond-count">{{ userInfo.score }}</view>
			<view class="tips">
				<view class="dot"></view>
				钻石可用于解锁私聊、解锁微信、解锁相册等功能，
				敬请期待。
			</view>
		</view>
		<view class="content">
			<view class="section-title">
				<view class="dot"></view>
				<text>钻石充值</text>
			</view>
			<view class="diamond-list">
				<view v-for="(item, index) in diamondList" :key="index"
					:class="['diamond-item', { active: selectedIndex === index }]" @click="handleSelect(index)">
					<image class="diamond-icon1" src="../../static/mine/icon_zs.png" />
					<text class="diamond-value">{{ Math.ceil(item.real_amount) }}</text>
					<text class="diamond-price">¥ {{ item.price }}</text>
				</view>
			</view>
		</view>
		<view class="footer">
			<selectPayType :days="days" type="zuanShi" @success="getUserInfo()"></selectPayType>
			<view class="agreement">
				<text class="agreement-text">支付即代表同意</text>
				<text class="agreement-link"
					@click="$Tips('/pages/richText/richText?biaoshi=value_added_services')">《缘定缘分增值服务协议》</text>
			</view>
		</view>
	</view>
</template>
<script>
	// #ifdef H5
	import index from '../../store'
	// #endif
	import selectPayType from '../../components/selectPayType/selectPayType.vue'
	export default {
		components:{
			selectPayType
		},
		computed:{
			days(){
				return this.diamondList[this.selectedIndex]?.id || '0'
			}
		},
		data() {
			return {
				userInfo: {
					money: ''
				},
				selectedIndex: 1,
				diamondList: [],
				changePayTypeName: '微信支付'
			}
		},
		onLoad() {
			this.getUserInfo()
			this.rechargeConfig()
		},
		methods: {
			handleSelect(index) {
				this.selectedIndex = index;
			},
			changePayType() {
				if (this.changePayTypeName == '微信支付') {
					this.changePayTypeName = '支付宝支付'
				} else {
					this.changePayTypeName = '微信支付'
				}
			},
			getUserInfo() {
				this.$Api['user'].getUserInfo().then(res => {
					this.userInfo = res.data
				})
			},
			rechargeConfig() {
				this.$Api['user'].rechargeConfig().then(res => {
					this.diamondList = res.data.recharge_config
				})
			},
			submitVipInfo() {
				//  #ifdef H5
				this.$Api['user'].moneyRecharge({
					recharge_id: this.diamondList[this.selectedIndex].id,
					paytype: 'wechat',
					method: 'mp'
				}).then(payres => {
					console.log(payres, 'payres')
					wxsdk.wxpay(payres.data, (res) => {
						if (res.errMsg == "chooseWXPay:ok") {
							this.$Toast('支付成功');
							this.getVipInfo()
						} else {
							this.$Toast('支付失败');
						}
					});
				})
				// #endif
				//  #ifdef APP
				if (this.changePayTypeName == '支付宝支付') {
					this.$Api['user'].moneyRecharge({
						recharge_id: this.diamondList[this.selectedIndex].id,
						paytype: 'alipay',
						method: 'app'
					}).then(payres => {
						uni.requestPayment({
							provider: 'alipay',
							orderInfo: payres.data, //微信、支付宝订单数据
							success: (res) => {
								this.$Toast('支付成功');
								this.getVipInfo()
							},
							fail: (err) => {
								this.$Toast('支付失败');
							}
						})
					})
				} else if (this.changePayTypeName == '微信支付') {
					this.$Api['user'].moneyRecharge({
						recharge_id: this.diamondList[this.selectedIndex].id,
						paytype: 'wechat',
						method: 'app'
					}).then(payres => {
						console.log(payres, 'payres')
						let params = JSON.parse(payres.data)
						console.log(params, 'params');
						uni.requestPayment({
							provider: 'wxpay',
							"orderInfo": {
								"appid": params.appid, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
								"noncestr": params.noncestr, // 随机字符串
								"package": params.package, // 固定值
								"partnerid": params.partnerid, // 微信支付商户号
								"prepayid": params.prepayid, // 统一下单订单号
								"timestamp": String(params.timestamp), // 时间戳（单位：秒）
								"sign": params.sign // 签名，这里用的 MD5/RSA 签名
							},
							success: (res) => {
								this.$Toast('支付成功');
								this.getVipInfo();
							},
							fail: (err) => {
								console.error('支付失败详情:', err); // 打印完整错误信息
								this.$Toast('支付失败');
							}
						});
					})
				}
				// #endif
			},
		}
	}
</script>
<style lang="scss" scoped>
	page {
		height: 100%;
		background-color: #F8F8F8;
	}

	.diamond-page {
		min-height: 100%;
		padding: 34rpx;
		display: flex;
		flex-direction: column;

		.diamond-icon {
			width: 30rpx;
			height: 30rpx;
		}

		.diamond-icon1 {
			width: 40rpx;
			height: 40rpx;
		}
	}

	.header {
		border-radius: 16rpx;
		padding:26rpx 22rpx 42rpx 30rpx ;
		margin-bottom: 30rpx;
		background: linear-gradient(180deg, #FFDAF5 0%, #FFFFFF 10%);
		box-shadow: 0rpx 4rpx 32rpx 0rpx #FFDAF5;

		.title-wrapper {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;

			.diamond-title {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #9A9A9A;

				.title {
					margin: 0 8rpx;
				}
			}

			.detail-btn {
				background: #F169F6;
				padding: 4rpx 24rpx;
				border-radius: 30rpx;

				.detail-text {
					font-size: 28rpx;
					line-height: 40rpx;
					color: #FFFFFF;
				}
			}
		}

		.diamond-count {
			font-size: 92rpx;
			font-weight: bold;
			margin: 20rpx 0;
		}

		.tips {
			display: flex;
			align-items: start;
			font-size: 26rpx;
			color: #999999;
			line-height: 1.5;

			.dot {
				margin-top: 14rpx;
				margin-right: 10rpx;
			}
		}
	}

	.dot {
		width: 12rpx;
		height: 12rpx;
		background: #F169F6;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(241, 105, 246, 0.5);
		border-radius: 8rpx;
		margin-right: 10rpx;
	}

	.content {
		border-radius: 16rpx;
		flex: 1;

		.section-title {
			display: flex;
			align-items: center;
			margin-bottom: 30rpx;
			font-size: 28rpx;
			color: #333333;
			font-weight: bold;
		}

		.diamond-list {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			grid-auto-flow: row;
			grid-gap: 20rpx;

			.diamond-item {
				width: 210rpx;
				height: 210rpx;
				box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(241, 105, 246, 0.4);
				border-radius: 12rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				box-sizing: border-box;
				background: #FFFFFF;

				&.active {
					background: #FFE9FC;
					box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(241, 105, 246, 0.4);
					border: 2rpx solid #F169F6;
				}

				.diamond-value {
					font-size: 48rpx;
					font-weight: bold;
					color: #333333;
					margin-top: 10rpx;
					margin-bottom: 18rpx;
				}

				.diamond-price {
					font-size: 28rpx;
					color: #9A9A9A;
				}
			}
		}
	}

	.footer {
		margin-top: 160rpx;

		.button-group {
			.image {
				width: 38rpx;
				height: 32rpx;
				margin-right: 20rpx;
			}

			.title {
				display: block;
				color: #ffffff;
				text-decoration: none;
			}

			.button1 {
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 0 auto;
				width: 600rpx;
				height: 88rpx;
				font-weight: bold;
				font-size: 30rpx;
				color: #FFFFFF;
				text-align: center;
				line-height: 88rpx;
				background: linear-gradient(135deg, #FE93BF 0%, #A27AFF 100%);
				border-radius: 44rpx;
			}
		}


		.agreement {
			text-align: center;
			margin-top: 20rpx;
			.agreement-text {
				font-size: 24rpx;
				color: #999999;
			}

			.agreement-link {
				font-size: 24rpx;
				color: #F169F6;
			}
		}
	}
</style>